﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace blackhouse.Web.FasterFiles
{
    [ToolboxData("<{0}:FasterFilesControl runat=server></{0}:FasterFilesControl>")]
    public class FasterFilesControl : WebControl
    {

        #region Event Handlers

        protected override void OnPreRender(EventArgs e)
        {
            if (FasterFilesProvider.HaveCssContent())
                addCssReference();
            if (FasterFilesProvider.HaveJsContent())
                addJsReference();
            base.OnPreRender(e);
        }

        #endregion

        #region Methods

        private void addCssReference()
        {
            if (Page.Header == null)
                throw new InvalidOperationException("empty head");
            HtmlLink link = new HtmlLink();
            link.Attributes.Add("rel", "stylesheet");
            link.Attributes.Add("type", "text/css");
            link.Href = "FasterFiles.css?cssid=" + FasterFilesProvider.GetHash();
            Page.Header.Controls.Add(link);
        }

        private void addJsReference()
        {
            if (!Page.ClientScript.IsClientScriptIncludeRegistered("fasterFiles"))
                Page.ClientScript.RegisterClientScriptInclude("fasterFiles", "FasterFiles.js?jsid=" + FasterFilesProvider.GetHash());
        }

        #endregion

    }
}